package main

import (
	"fmt"
	"sync"
)

// User 定义用户结构体
type User struct {
	ID   int
	Name string
	Age  int
}

// 使用sync.Once确保只初始化一次
var (
	userInstance *User
	userOnce     sync.Once
)

// GetUserInstance 获取User的单例实例
func GetUserInstance() *User {
	userOnce.Do(func() {
		userInstance = &User{}
	})
	return userInstance
}

// SetUserInfo 设置用户信息
func (u *User) SetUserInfo(id int, name string, age int) {
	u.ID = id
	u.Name = name
	u.Age = age
}

// GetUserInfo 获取用户信息
func (u *User) GetUserInfo() string {
	return fmt.Sprintf("ID: %d, Name: %s, Age: %d", u.ID, u.Name, u.Age)
}

func main() {
	// 获取单例实例
	user1 := GetUserInstance()
	user1.SetUserInfo(1, "张三", 25)

	// 再次获取实例
	user2 := GetUserInstance()

	// 验证是否为同一个实例
	fmt.Println("user1 == user2:", user1 == user2)
	fmt.Println("User Info:", user2.GetUserInfo())

	// 修改用户信息
	user2.SetUserInfo(2, "李四", 30)
	fmt.Println("Updated User Info:", user1.GetUserInfo())
}